var theId
cateListAPI({}, function (res) {
    var arr = res.data.data;
    arr.forEach(function (obj) {
        var option = `<option value="${obj.Id}">${obj.name}</option>`;
        $('select[name=cate_id]').append(option);
        layui.use('form', function () {
            var form = layui.form;
            form.render();

        })
    });
    var theId = window.location.search.slice(4);
    if (theId) {
        getArgByIdAPI(theId, function (res) {
            var form = layui.form;
            form.val('editForm', res.data.data);
            cropper.replace(res.data.data.cover_img)
            tinymce.init({
                selector: "#textarea",
                language: "zh_CN",
                plugins: 'table advlist autolink link image lists preview'
            });
        })
    } else {
        tinymce.init({
            selector: "#textarea",
            language: "zh_CN",
            plugins: 'table advlist autolink link image lists preview'
        });
    }
})



const image = document.getElementById('image');
const cropper = new Cropper(image, {
    aspectRatio: 1,
    preview: $(".img-preview")
});

$('.layui-form').on('submit', function (ev) {
    ev.preventDefault();
    var fd = new FormData($('.layui-form')[0]);
    var con = tinyMCE.activeEditor.getContent();
    fd.set('content', con);
    var canvas = cropper.getCroppedCanvas({
        width: 100,
        height: 100
    });
    canvas.toBlob(function (blob) {
        fd.append("cover_img", blob);
        // fd.forEach(function (val, key) {
        //     console.log(val, key);
        // })
        if (theId) {
            fd.append('Id', theId);
            upArgByIdAPI(fd, function (res) {
                console.log(res)
            })
        } else {
            addArtAPI(fd, function (res) {
                console.log(res)
            })
        }

    })


})

$(".select").on('click', function () {
    $('#file').click();
})
//文件域的改变事件
$('#file').on('change', function (ev) {
    var imgURL = URL.createObjectURL(ev.target.files[0]);
    cropper.replace(imgURL)
})